﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BLL;
public partial class CustommerInfo : System.Web.UI.Page
{
    HoaDonChiTiet_BLL HDCT = new HoaDonChiTiet_BLL();
    public static string so1, so2, ketqua;
    MaHoaMK_BLL MaHoa = new MaHoaMK_BLL();
    HoaDon_BLL HD = new HoaDon_BLL();
    KhachHang_BLL CustomerBLL = new KhachHang_BLL();
    DataTable dt = new DataTable();
    public static string TenKH;
    public static string MaKH = string.Empty;
    public static string HoaDonID;
    public static DataTable dtb = new DataTable();
    public static DataTable dtbls = new DataTable();


    protected void Page_Load(object sender, EventArgs e)
    {

        if (Session["Khach"] != null)
        {
            TenKH = Session["Khach"].ToString();
        }
        else {
            Response.Redirect("Home.html");
        }
        if (!IsPostBack)
        {
            TaoMa();
            dt = CustomerBLL.KhachHang_ViewTable(1, TenKH + "");
            if (dt.Rows.Count > 0)
            {            
                MaKH = dt.Rows[0]["CustommerID"].ToString();
                string[] NS = dt.Rows[0]["Birthday"].ToString().Split('/');
                txtMaKH.Text = dt.Rows[0]["CustommerID"].ToString();
                txtTenDN.Text = dt.Rows[0]["LoginName"].ToString();
                txtEmail.Text = dt.Rows[0]["Email"].ToString();
                txtHoTen.Text = dt.Rows[0]["FullName"].ToString();
                txtDienThoai.Text = dt.Rows[0]["PhoneNumber"].ToString();
                txtDiaChi.Text = dt.Rows[0]["Address"].ToString();                
                RadioBtnNgaySinh.SelectedValue = dt.Rows[0]["Sex"].ToString();
                DropNgay.SelectedValue = (string)NS[0];
                DropThang.SelectedValue = (string)NS[1];
                txtNam.Text = (string)NS[2];                
            }
        }
    }
    protected void LinkbtnTaiKhoan_Click(object sender, EventArgs e)
    {
        lblThongBao1.Text = "";
        lblThongBao2.Text = "";
        PanelTaiKhoan.Visible = true;
        PanelDoiMatKhau.Visible = false;
        PanelGiaoDich.Visible = false;
        PanelChiTiet.Visible = false;
    }
    protected void LinkbtnDoiMatKhau_Click(object sender, EventArgs e)
    {
        lblThongBao1.Text = "";
        lblThongBao2.Text = "";
        PanelTaiKhoan.Visible = false;
        PanelDoiMatKhau.Visible = true;
        PanelGiaoDich.Visible = false;
        PanelChiTiet.Visible = false;
    }
    protected void LinkbtnGiaoDich_Click(object sender, EventArgs e)
    {
        ViewGrid();
        lblThongBao1.Text = "";
        lblThongBao2.Text = "";
        PanelTaiKhoan.Visible = false;
        PanelDoiMatKhau.Visible = false;
        PanelGiaoDich.Visible = true;
        PanelChiTiet.Visible = false;
    }
    void ViewGrid()
    {
        //dtbls = HD.HoaDon_UserView(MaKH);
        dtbls = new DataTable();
        dtbls = new CmsBLL().CartOrder_GetList(MaKH, string.Empty, string.Empty);

        if (dtbls.Rows.Count > 0)
        {
            GridViewLichSuGiaoDich.DataSource = dtbls;
            
        }
        GridViewLichSuGiaoDich.DataBind();
    }

    protected void btnTuBo_Click(object sender, EventArgs e)
    {
        PanelTaiKhoan.Visible = false;
    }
    protected void btnTuChoi_Click(object sender, EventArgs e)
    {
        PanelDoiMatKhau.Visible = false;
    }
    protected void btnThayDoi_Click(object sender, EventArgs e)
    {
        if (txtMatKhauCu.Text == "")
            ScriptManager.RegisterStartupScript(this, this.GetType(), "RederectMe", "alert('Nhập vào mật khẩu cũ');", true);
        else
            if (txtMatKhauMoi.Text == "")
                ScriptManager.RegisterStartupScript(this, this.GetType(), "RederectMe", "alert('Nhập vào mật khẩu mới');", true);
            else
                if (txtMatKhauMoi.Text + "" != txtReMatKhauMoi.Text + "")
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "RederectMe", "alert('Xác nhận mật khẩu không đúng');", true);
                else
                {
                    string error = "";
                    string MKCu = MaHoa.GetMD5(txtMatKhauCu.Text);
                    string MKMoi = MaHoa.GetMD5(txtMatKhauMoi.Text);
                    try
                    {
                        CustomerBLL.KhachHang_DoiMatKhau(0, TenKH, MKCu, MKMoi, "", ref error);
                        if (error + "" != "")
                            ScriptManager.RegisterStartupScript(this, this.GetType(), "RederectMe", "alert('" + error + "');", true);
                        else
                        {
                            lblThongBao2.Text = "Cập nhật mật khẩu thành công!";
                        }
                           
                    }
                    catch
                    { 
                       ScriptManager.RegisterStartupScript(this, this.GetType(), "RederectMe", "alert('Lỗi không thực hiện được');", true);
                    }
                }
    }
    public void TaoMa()
    {
        string strString = "0123456789";
        Random random = new Random();
        int randomCharIndex = 0;
        char randomChar;
        for (int i = 0; i < 1; i++)
        {
            randomCharIndex = random.Next(0, strString.Length);
            randomChar = strString[randomCharIndex];
            so1 = Convert.ToString(randomChar);
        }
        for (int i = 0; i < 1; i++)
        {
            randomCharIndex = random.Next(0, strString.Length);
            randomChar = strString[randomCharIndex];
            so2 = Convert.ToString(randomChar);
        }
        lblcauhoi.Text = "<font color='red'>" + so1 + "</font> + <font color='red'>" + so2 + "</font> =?";
        ketqua = (int.Parse(so1) + int.Parse(so2)).ToString();
    }
    protected void btnCapNhat_Click(object sender, EventArgs e)
    {
        if (ketqua != txtTraLoi.Text)
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "RedirectMe", "alert('Câu trả lời không đúng!');", true);
            TaoMa();
        }
        else
        {
            string error = "";        
            string NS = DropNgay.SelectedValue + "/" + DropThang.SelectedValue + "/" + txtNam.Text;
            DateTime NgaySinh = new DateTime();
            string GioiTinh = RadioBtnNgaySinh.SelectedValue;
            NgaySinh = DateTime.ParseExact(NS,"dd/MM/yyyy",null);
            try
            {
                CustomerBLL.UpdateCustomer(MaKH, txtHoTen.Text, txtDienThoai.Text,txtDiaChi.Text,GioiTinh,NgaySinh, ref error);
                if (error + "" != "")
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('" + error + "');", true);
                }
                else
                {
                    lblThongBao1.Text = "Cập nhật thông tin thành công!";
                }
            }
            catch
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('Lỗi! không thực hiện được');", true);
            }
        }
    }
    protected void GridViewLichSuGiaoDich_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "PrintOrder")
        {
            string HoaDonID = e.CommandArgument.ToString();
            Redirect("~/ViewOrder.aspx?HoaDonID="+HoaDonID+"", "_blank", "menubar=0,height=600,width=900");
        }
        if (e.CommandName == "ViewOrder")
        {
            HoaDonID = e.CommandArgument.ToString();
            lblThongBao1.Text = "";
            lblThongBao2.Text = "";
            PanelTaiKhoan.Visible = false;
            PanelDoiMatKhau.Visible = false;
            PanelGiaoDich.Visible = false;
            PanelChiTiet.Visible = true;
            PanelChiTiet.GroupingText = "<font color=Blue><b>Chi tiết đơn hàng số: " + HoaDonID.ToString() + "</b></font>";
            try
            {
                //ViewDuLieu();
                ViewGridSP();
            }
            catch
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Alert", "alert('Load dữ liệu lỗi');", true);
            }
        }
    }
    protected void GridViewLichSuGiaoDich_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridViewLichSuGiaoDich.PageIndex = e.NewPageIndex;
        ViewGridSP();
    }
    protected void lbtClose_Click(object sender, EventArgs e)
    {
        lblThongBao1.Text = "";
        lblThongBao2.Text = "";
        PanelTaiKhoan.Visible = false;
        PanelDoiMatKhau.Visible = false;
        PanelGiaoDich.Visible = true;
        PanelChiTiet.Visible = false;
    }
    void ViewDuLieu()
    {
        //DataTable dt = HD.HoaDon_ViewDuLieu(1, HoaDonID);
        //DateTime NgayLap;
        if (dt.Rows.Count > 0)
        {
            //NgayLap = (DateTime)dt.Rows[0]["NgayLap"];
            //lblNgayTaoHD.Text = NgayLap.ToString("dd/MM/yyyy");
            //lblTinhTrangHD.Text = dt.Rows[0]["TenTinhTrang"].ToString();          
            //if (dt.Rows[0]["Tra_GioiTinh"].ToString() == "Nam")
            //    T_GioiTinh.Text = "Nam";
            //else
            //    T_GioiTinh.Text = "Nữ";
            //T_HoTen.Text = dt.Rows[0]["Tra_HoTen"].ToString();
            //T_CongTy.Text = dt.Rows[0]["Tra_CongTy"].ToString();
            //T_DiaChi.Text = dt.Rows[0]["Tra_DiaChi"].ToString();
            //T_DienThoai.Text = dt.Rows[0]["Tra_DienThoai"].ToString();
            //T_ThanhPho.Text = dt.Rows[0]["Tra_ThanhPho"].ToString();
            //T_Tinh.Text = dt.Rows[0]["Tra_HuyenTinh"].ToString();
            //T_MaVung.Text = dt.Rows[0]["Tra_MaVung"].ToString();
            //T_TenNuoc.Text = dt.Rows[0]["Tra_TenNuoc"].ToString();
            //if (dt.Rows[0]["Nhan_GioiTinh"].ToString() == "Nam")
            //    N_GioiTinh.Text = "Nam";
            //else
            //    N_GioiTinh.Text = "Nữ";
            //N_HoTen.Text = dt.Rows[0]["Nhan_HoTen"].ToString();
            //N_CongTy.Text = dt.Rows[0]["Nhan_CongTy"].ToString();
            //N_DiaChi.Text = dt.Rows[0]["Nhan_DiaChi"].ToString();
            //N_DienThoai.Text = dt.Rows[0]["Nhan_DienThoai"].ToString();
            //N_ThanhPho.Text = dt.Rows[0]["Nhan_ThanhPho"].ToString();
            //N_Tinh.Text = dt.Rows[0]["Nhan_HuyenTinh"].ToString();
            //N_MaVung.Text = dt.Rows[0]["Nhan_MaVung"].ToString();
            //N_TenNuoc.Text = dt.Rows[0]["Nhan_TenNuoc"].ToString();
            //lblPhuongThucTT.Text = dt.Rows[0]["TenPhuongThuc"].ToString();        
            //lblComment.Text = dt.Rows[0]["Comment"].ToString();
        }
    }
    void ViewGridSP()
    {
        //dtb =  HDCT.HoaDonCT_ViewDL(HoaDonID);
        dtb = new BLL.CmsBLL().GetOrderDetail(HoaDonID);
        if (dtb.Rows.Count > 0)
        {
            GridCart.DataSource = dtb;
            GridCart.DataBind();
            Total();
        }
    }
    public void Total()//==>kai nay tinh tong tien nhu vua nay
    {
        double TongGia = 0;
        foreach (DataRow dr in dtb.Rows) //lay tat ca gia tri trong bang
        {
            TongGia += double.Parse(dr[6].ToString());
        }
        GridCart.FooterRow.Cells[3].Text = TongGia.ToString("0,0") + " vnđ";
        //lblTongHD.Text = TongGia.ToString("0,0") + " vnđ";
    }
    public void Redirect(string url, string target, string windowFeatures) 
    { 
        if ((String.IsNullOrEmpty(target) || target.Equals("_self", StringComparison.OrdinalIgnoreCase)) && String.IsNullOrEmpty(windowFeatures)) 
        { 
            Response.Redirect(url); 
        } else 
        { 
            Page page = (Page)HttpContext.Current.Handler; 
            if (page == null) 
            { 
                throw new InvalidOperationException("Cannot redirect to new window outside Page context."); 
            } 
            url = page.ResolveClientUrl(url); 
            string script; 
            if (!String.IsNullOrEmpty(windowFeatures)) 
            { 
                script = @"window.open(""{0}"", ""{1}"", ""{2}"");"; 
            } 
            else 
            { 
                script = @"window.open(""{0}"", ""{1}"");"; 
            } 
            script = String.Format(script, url, target, windowFeatures); 
            ScriptManager.RegisterStartupScript(page, typeof(Page), "Redirect", script, true); 
        } 
    }
}
